let BaseController = require('./BaseController')
let fs = require('fs')
let path = require('path')
let cheerio = require('cheerio')
class Controller extends BaseController {
    constructor() {
        super();
        this.baseSrc = `d:/数据指标分类/${new Date().format('yyyyMMdd')}/待入库/11指数/中国股市指数/`;
        this.encode = 'UTF-8'
        this.params = {
            pageIndex: 1,
            pageTotal: ''
        }


        this.data = []
    };
    /**
     * 获取url
     */
    getUrl(data) {
        if (data.type === 'list') {
            return `http://www.iwencai.com/stockpick/search?typed=1&preParams=&ts=1&f=3&qs=result_original&selfsectsn=&querytype=stock&searchfilter=&tid=stockpick&w=%E4%B9%B0%E5%85%A5%E4%BF%A1%E5%8F%B7&queryarea=`
        } else {
            return `http://www.iwencai.com/diag/block-detail?pid=11295&codes=${data.code}&codeType=stock&info=%7B%22view%22%3A%7B%22nolazy%22%3A1%2C%22parseArr%22%3A%7B%22_v%22%3A%22new%22%2C%22dateRange%22%3A%5B%2220180808%22%2C%2220180808%22%5D%2C%22staying%22%3A%5B%5D%2C%22queryCompare%22%3A%5B%5D%2C%22comparesOfIndex%22%3A%5B%5D%7D%2C%22asyncParams%22%3A%7B%22tid%22%3A9271%7D%7D%7D`
        }


    }

    async getParams() {
        let result = await this.getData({ type: 'list' }, false, 'get')
        this.paramsList = this.axiosDataFormat(result.data, 'list')
    };

    /**
     * 获取历史数据
     */
    async getHistory() {
        console.log('41买入信号-ajax-Controller')
        for (let i = 0; i < 5; i++) {
            let code = this.paramsList[i].code;
            let result = await this.getData({ type: 'detail', code }, false, 'get')
            console.log(result.data)
        }
    };

    /**
     * 数据格式化
     */
    axiosDataFormat(data, type) {
        let result = null
        if (type === 'list') {
            let $ = cheerio.load(data, { decodeEntities: true });
            let arr = []
            $('.static_table.tbody_table.static_tbody_table tr').map((i, tr) => {
                let code = $(tr).find('a').attr('href').match(/\d+$/)[0];
                let name = $(tr).find('a').text().trim();
                let href = $(tr).find('a').attr('href');
                arr.push({ code, name, href })
            })
            result = arr
        }
        return result
    }

    /**
     * 写csv
     */
    writeCsv(arr) {
        if (!fs.existsSync(this.baseSrc)) {
            this.mkdirs(this.baseSrc)
        }
        if (arr && arr.length) {
            let content = '序号,代码,名称,最新价,涨跌额,涨跌幅,-,成交量,成交额,昨收,今开,最高,最低,-,-,-,-,-,-,-,-,-,-,-,-,-\n'
            content += arr
            fs.writeFileSync(path.join(this.baseSrc, `中国股市指数_${new Date().format('yyyyMMdd')}.csv`), content, 'UTF-8')
        }
    }

    async main(startDate = '2015-01-01', isCache = true) {this.startDate = startDate;this.isCache = isCache;
        await this.getParams()
        await this.getHistory()
            // this.writeCsv(this.data);
    }
}

let controller = new Controller()
controller.main()
    // module.exports = controller